<?php

//db操作
$app->HandleFunc("/db",function() {
    global $config;
    //$db1 = DbMysql::getInstance($config["db"]);
    //$db2 = new DbMysql($config["db"]);
    //$db3 = DbMysql::getInstance($config["db"]);

    $list = $this->db->get_all("select user_id from fa_online limit 1");
    //print_r($list);
    $this->ServerJson($list);
});

//注册路由api/test
$app->HandleFunc("/api/test",function() {
    $this->ServerHtml("api test hello");
});

//注册路由hello
$app->HandleFunc("/hello",function() {
    $this->ServerHtml("Hello World WorkerMan WebWorker!");
});

//注册路由json
$app->HandleFunc("/json",function() {
     //以json格式响应
     $this->ServerJson(array("name"=>"WebWorker"));
});

//注册路由/
$app->HandleFunc("/",function() {
     //自定义响应头
     $this->Header("server: xtgxiso");
     //设置cookie
     $this->Setcookie("xtgxiso",time()); 
     //以json格式响应
     $this->ServerJson(array("name"=>"WebWorker"));
});

//注册路由input
$app->HandleFunc("/input",function() {
    //获取body
     $body = $GLOBALS['HTTP_RAW_POST_DATA'];
     $this->ServerHtml($body);
});


//redis示例
$app->HandleFunc("/redis",function() use($app,$config){
     $redis = Mredis::getInstance($config["redis"]);
     $app->ServerHtml($redis->get("xtgxiso"));
});

//mysql示例
$app->HandleFunc("/mysql",function() use($app,$config){
     $this->db = Mdb::getInstance($config["db"]);
     $result = array();
     $sql = "select * from test limit 1";
     //取一行对象结果集
     $result['data1'] = $this->db->query($sql)->row();
     //取一行数组结果集
     $result['data2'] = $this->db->query($sql)->row_array();
     //取多行对象结果集
     $result['data3'] = $this->db->query($sql)->result();
     //取多行数组结果集
     $result['data4'] = $this->db->query($sql)->result_array();
     //取多行，自动转义参数
     $result['data5'] = $this->db->query("select * from test where id = ? or id =? ",array(1,2))->result_array();
     //取表test中的一行数据
     $result['data6'] = $this->db->get("test",0,1)->row_array();
     //取表中id=22的一行数据
     $result['data7'] = $this->db->get_where("test",array("id"=>22),0,1)->row_array();
     //向表test插入数据
     $result['data8'] = $this->db->insert("test",array("name"=>time()));
     //更新表test中的id=1的数据
     $result['data9'] = $this->db->update("test",array("name"=>time()),array("id"=>1));
     //删除表test中的id=2的数据
     $result['data10'] = $this->db->delete("test",array("id"=>2));
     $this->ServerJson($result);
    
});

//自定义404
$app->on404  = function() {
    $this->ServerHtml("我的404");
};

//测试类
class xtgxiso{
    static function test()
    {
        global $app;
        $app->ServerHtml("xtgxiso");
    }
}
//注册类
$app->HandleFunc("/xtgxiso",array("xtgxiso","test"));

//订阅，不考虑分布式，分布式的话连接不能存储在内存中
$app->HandleFunc("/subscription",function() {
    $id = $_GET["id"];
    $this->conn_list[$id] =  $this->conn;
    $this->conn_close = false;//通过设置属性控制连接不关闭
});

//发布，不考虑分布式，分布式的话连接不能存储在内存中
$app->HandleFunc("/publish",function() {
    $id = $_GET["id"];
    $conn = $this->conn_list[$id];
    if ( $conn ){
        $str = $_GET["content"];
        $conn->send($str);
    }
    $this->ServerHtml("");
});